﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Garlic.CC.BaseClass;
using Garlic.CC.Utility;
using Garlic.BLL.Common;
using Garlic.DM.DataModel;
using Garlic.CC.WebUtility;
using Garlic.BLL;
using System.Collections.Generic;
using System.Text;


public partial class SystemManagement_OperationLogList : BasePage
{
    

    protected void Page_Load(object sender, EventArgs e)
    {
            if (!Page.IsPostBack)
            {
                this.tbOperaton_BeginTime.Text = DateTime.Parse(DateTime.Now.ToShortDateString()).ToString("yyyy-MM-dd HH:mm:ss");
                this.tbOperation_EndTime.Text = DateTime.Parse(DateTime.Now.AddDays(1).ToShortDateString()).ToString("yyyy-MM-dd")+" 23:59:59";

                DataTable TableSearchType1 =OperationLogManager.GetSearType(1);
                Modelselect.DataSource = TableSearchType1;
                Modelselect.DataTextField = TableSearchType1.Columns[0].ToString();
                Modelselect.DataValueField = TableSearchType1.Columns[0].ToString();                
                Modelselect.DataBind();
                Modelselect.Items.Insert(0, new ListItem(DropDownListSpecialSymbolDefine.SelectAllText, ""));
               
                DataTable TableSearchType2 = OperationLogManager.GetSearType(2);
                Actionselect.DataSource = TableSearchType2;
                Actionselect.DataTextField = TableSearchType2.Columns[0].ToString();
                Actionselect.DataValueField = TableSearchType2.Columns[0].ToString();
                Actionselect.DataBind();
                Actionselect.Items.Insert(0, new ListItem(DropDownListSpecialSymbolDefine.SelectAllText, ""));

            }
        
    }

    #region method

    /// <summary>
    /// 初始化查询栏
    /// </summary>
    private void InitSearchBar()
    {
        //初始化查询列表

        //List<PlantInfo> plants = plantMgr.GetListByUserID();
        //List<WorkshopInfo> workshops = workshopMgr.GetListByUserID();
        //List<SupplierInfo> suppliers = SupplierManager.GetListWithWorkshop();

        //this.PartSearchBar.ListSourcXYbles = ...
    }


    void InitPartActivity()
    {
    }

    void InitPartType()
    { }


    private void Search()
    {
        
        StringBuilder conditionBuilder = new StringBuilder();
        if (this.tbOperaton_BeginTime.Text.Trim() != "")
        {
            conditionBuilder.AppendFormat(" operation_time >= '"+tbOperaton_BeginTime.Text+"'");
        }


        if (this.tbOperation_EndTime.Text.Trim() != "" )
        {
            if(conditionBuilder.ToString().Equals(""))
                conditionBuilder.AppendFormat(" operation_time <=  '" + tbOperation_EndTime.Text + "'");
            else
                conditionBuilder.AppendFormat(" AND operation_time <=  '"+ tbOperation_EndTime.Text+"'");
        }

        if (this.Modelselect.Text.Trim().Length > 0)
        {
            if (conditionBuilder.ToString().Equals(""))
                conditionBuilder.AppendFormat(" [operation_parameter1] like  '" + this.Modelselect.Text + "%'");
            else
                conditionBuilder.AppendFormat(" AND [operation_parameter1] like  '" + this.Modelselect.Text + "%'");
        }
        if (this.Actionselect.Text.Trim().Length > 0)
        {
            if (conditionBuilder.ToString().Equals(""))
                conditionBuilder.AppendFormat(" [operation_parameter1] like  '%" + this.Actionselect.Text + "'");
            else
                conditionBuilder.AppendFormat(" AND [operation_parameter1] like  '%" + this.Actionselect.Text + "'");
        }
        if (!this.operationDXYil.Text.Trim().Equals(""))
        {
            if (conditionBuilder.ToString().Equals(""))
                conditionBuilder.AppendFormat(" [operation_dXYil] like  '%" + this.operationDXYil.Text + "%'");
            else
                conditionBuilder.AppendFormat(" and [operation_dXYil] like  '%" + this.operationDXYil.Text + "%'");
        }

        string condition = conditionBuilder.ToString();
        //if (!string.IsNullOrEmpty(condition))
        //{
        //    if (condition.LastIndexOf(" AND ") != -1)
        //        condition = condition.Substring(0, condition.LastIndexOf(" AND "));
        //}

        this.SmartGridView1.DataSourceID = "OperationLogSource";

        this.OperationLogSource.SelectParameters["textWhere"].DefaultValue = condition;


    }

    private void ShowMessage(string msg)
    {
        this.Message = msg;
    }

    #endregion

    #region toolbar event handler
    /// <summary>
    /// 工具栏事件处理
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="args"></param>
    protected void TopToolBar_OnClick(object sender, Garlic.UI.Components.IconButtonClickEventArgs args)
    {
        switch (args.Command)
        {
            case ToolbarCommand.Search:
                Search();
                this.pnlSearch.Visible = false;
                break;
            case "Hide":
                this.pnlSearch.Visible = !this.pnlSearch.Visible;
                break;

            case ToolbarCommand.Export:
                ExportExcel();
                break;
        }

    }
    #endregion

    private void ExportExcel()
    {
        string filename = "dock";
        string message;
        bool isSuccess = Garlic.Infrustructure.Utilities.WebUtil.Export(false, SmartGridView1, ref filename, out message);
        if (isSuccess)
            OutputFilePath = filename;
        else
            Message = message;

    }


    //protected void SmartGridView1_DataBound(object sender, EventArgs e)
    //{
    //     List<CodeDetailInfo> CodeDetailActionNameInfo = CodeDetailManager.GetList("code_name = 'action_info'", "");

    //     foreach (GridViewRow row in this.SmartGridView1.Rows)
    //     {
    //         //翻译动作名称            
    //         if (CodeDetailActionNameInfo != null)
    //         {
    //             for (int i = 0; i < CodeDetailActionNameInfo.Count; i++)
    //             {
    //                 if (CodeDetailActionNameInfo[i].Code_value == row.Cells[2].Text)
    //                 {
    //                     row.Cells[2].Text = string.Format(CodeDetailActionNameInfo[i].Value_cdescription);
    //                     break;
    //                 }
    //             }
    //         }
    //     }
    //}


    protected void SmartGridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            GridViewRow row = e.Row;

            string urlBrowse = "openWindow('OperationLogDXYil.aspx?operation_log_sn={0}',700,320,'浏览详细信息','yes');";
            HyperLink linkBrowse = (HyperLink)(e.Row.FindControl("hlBrowse"));

            if (linkBrowse != null)
            {
                linkBrowse.NavigateUrl = "#";
                // linkCopy.Text = row.Cells[2].Text;
                linkBrowse.Attributes.Add("onclick", string.Format(urlBrowse, linkBrowse.Text));
            }
        }
    }
}